#region

using System;
using System.Linq;
using System.Threading;
using System.Web.Mvc;
using log4net;
using xConnected.Common.Education;
using xConnected.Core.Model;
using xConnected.Service;

#endregion

namespace xConnected.Controllers
{
    public class EducationController : BaseController<Education>
    {
        private readonly EducationService _educationService;

        public EducationController(EducationService educationService)
        {
            _educationService = educationService;
        }

        public JsonResult List(string search = "", int pageIndex = 0, int pageSize = 10)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var page = search != ""
                               ? _educationService.GetAllPagedSearched(search, pageIndex, pageSize)
                               : _educationService.GetAllPaged(pageIndex, pageSize);
                var educations = page.Entities.ToList().Select(e => new
                                                                        {
                                                                            e.Id,
                                                                            e.Created,
                                                                            e.Updated,
                                                                            e.Title,
                                                                            Description =
                                                                       EducationRes.ResourceManager.GetString(e.Key,
                                                                                                            Thread.
                                                                                                                CurrentThread
                                                                                                                .
                                                                                                                CurrentCulture),
                                                                            e.Key
                                                                        });

                return
                    Json(
                        new
                            {
                                search,
                                page.Count,
                                page.CurrentPage,
                                page.PageSize,
                                page.PagesCount,
                                Entities = educations
                            }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult Save(Education education)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                _educationService.SaveOrUpdate(education);
                return Json(education.Id, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult DeleteEducation(int id)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var education = _educationService.GetById(id);
                _educationService.Delete(education);
                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }


        public JsonResult GetEducationsAutocomplete(string searchTerm, int maxRows)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var educations = _educationService.GetAll().ToList().Select(e => new
                {
                    Description = EducationRes.ResourceManager.GetString(e.Key,Thread.CurrentThread.CurrentCulture),
                    e.Id
                });
                if (searchTerm != "")
                {
                    educations =
                        educations.Where(l => l.Description.ToLower().Contains(searchTerm.ToLower())).Take(maxRows);
                }
                return Json(educations, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}